<template>
    <div>
        <!-- seach区域 -->
        <div class="seach">
            <span class="seachStatus">
                {{$t('duedat')}}
                <el-date-picker :placeholder="$t('plsset')" v-model="transTime" width="20%" type="daterange" :range-separator="$t('to')" :start-placeholder="$t('startdate')" :end-placeholder="$t('dateclosed')" format="yyyy-MM-dd" value-format="yyyy-MM-dd" size="mini"></el-date-picker>
            </span>
            <span>
                {{$t('mertid')}}
                <el-input :placeholder="$t('plsipt')" v-model.trim="clarifyForm.merId" size="mini"></el-input>
            </span>
            <span class="seachCSC">
                {{$t('batchNo')}}
                <el-input v-model.trim="clarifyForm.batchNo" :placeholder="$t('plsset')" size="mini"></el-input>
            </span>
            <div class="NOUS">
                <el-button type="primary" @click="seach" size="mini" v-preventReClick>{{$t('qury')}}</el-button>
                <el-button type="success" @click="exportExcel" size="mini" v-preventReClick>{{$t('expot')}}</el-button>
            </div>
        </div>
        <!-- 内容区域 -->
        <el-card class="cardBox">
            <el-table border style="width: 100%" row-key="funcId" :data="clarifyTab" id="out-table">
                <el-table-column type="index" :label="$t('sernum')">
                </el-table-column>
                <el-table-column prop="merId" :label="$t('mertid')" min-width="184">
                </el-table-column>
                <el-table-column prop="merName" :label="$t('mertnam')" min-width="101">
                </el-table-column>
                <el-table-column prop="batchNo" :label="$t('batchNo')" min-width="179">
                </el-table-column>
                <el-table-column prop="shouldSettleDate" :label="$t('duedat')" min-width="132">
                </el-table-column>
                <el-table-column prop="settleDate" :label="$t('actsetldat')" min-width="166">
                </el-table-column>
                <el-table-column prop="settleAmt" :label="$t('setlamt')" min-width="144">
                </el-table-column>
                <el-table-column prop="status" :label="$t('setlsta')" min-width="148">
                </el-table-column>
                <el-table-column prop="settleCount" :label="$t('setlnum')" min-width="139">
                </el-table-column>
                <!-- <el-table-column prop="address" :label="$t('opratn')" width="90" fixed="right">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="detailsSetRec(scope.row)">{{$t('detls')}}</el-link>
                    </template>
                </el-table-column> -->
            </el-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="clarifyForm.pageNum"
                :page-sizes="[10, 15, 20, 30]"
                :page-size="clarifyForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import {
    setreclist
} from '@/libs/reqlist.js'
import { getInitDate } from '@/libs/getDate.js'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
    data () {
        return {
            transTime: [],
            total: 0,
            detDialogVisible: false,
            clarifyForm: {
                batchNo: null,
                merName: null,
                merOrderNo: null,
                payOrderNo: null,
                transStartTime: null,
                transEndTime: null,
                merId: null,
                pageNum: 1,
                pageSize: 10
            },
            clarifyTab: [],
            detsRecForm: {}
        }
    },
    methods: {
        getClarify () {
            setreclist(JSON.stringify(this.clarifyForm)).then(res => {
                if (res.data.code === '000000') {
                    this.total = res.data.data.total - 0
                    this.clarifyTab = res.data.data.list
                    this.clarifyTab.forEach(item => {
                        item.shouldSettleDate = item.shouldSettleDate.slice(0, 4) + '-' + item.shouldSettleDate.slice(4, 6) + '-' + item.shouldSettleDate.slice(6, 8)
                        if (typeof (item.settleDate) !== 'undefined' && item.settleDate !== null) {
                            item.settleDate = item.settleDate.slice(0, 4) + '-' + item.settleDate.slice(4, 6) + '-' + item.settleDate.slice(6, 8)
                        }
                        if (item.status === 0) item.status = this.$t('sett')
                        if (item.status === 1) item.status = this.$t('setttobepaid')
                        if (item.status === 2) item.status = this.$t('alredypaid')
                        if (item.status === 3) item.status = this.$t('failpaymentagn')
                        if (item.status === 4) item.status = this.$t('paying')
                    })
                }
            })
        },
        seach () {
            if (this.transTime !== null) {
                this.clarifyForm.transStartTime = this.transTime[0]
                this.clarifyForm.transEndTime = this.transTime[1]
                this.clarifyForm.transStartTime = this.clarifyForm.transStartTime.split('-').join('')
                this.clarifyForm.transEndTime = this.clarifyForm.transEndTime.split('-').join('')
            } else {
                this.clarifyForm.transStartTime = null
                this.clarifyForm.transEndTime = null
            }
            this.clarifyForm.pageNum = 1
            this.getClarify()
        },
        handleSizeChange (newSize) {
            this.clarifyForm.pageSize = newSize
            this.getClarify()
        },
        // 监听总页数变化
        handleCurrentChange (newNum) {
            this.clarifyForm.pageNum = newNum
            this.getClarify()
        },
        exportExcel () {
            let fix = document.querySelector('.el-table__fixed-right')
            let xlsxParam = { raw: true }
            /* generate workbook object from table */
            let wb
            if (fix) {
                wb = XLSX.utils.table_to_book(document.querySelector('#out-table').removeChild(fix), xlsxParam)
                document.querySelector('#out-table').appendChild(fix)
            } else {
                wb = XLSX.utils.table_to_book(document.querySelector('#out-table'), xlsxParam)
            }
            /* get binary string as output */
            let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
            try {
                FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream;charset=utf-8' }), '结算记录.xlsx')
            } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
            return wbout
        }
        // 展示详情
        // detailsSetRec (data) {
        //     this.$router.push('/sec/setRecDet')
        // }
    },
    mounted () {
        this.clarifyForm.transStartTime = getInitDate(7, 0).split('-').join('')
        this.clarifyForm.transEndTime = getInitDate(0, 0).split('-').join('')
        this.transTime.push(getInitDate(7, 0))
        this.transTime.push(getInitDate(0, 0))
        this.getClarify()
    }
}
</script>

<style lang="less" scoped>
@import './setRec.less';
</style>
